package com.wangzhenyu.learn.test.designPattern


//访问者模式
@Suppress("UNUSED")
object PVisitorPattern {

    abstract class Action {

        abstract fun getManResult(man: Man)

        abstract fun getWomanResult(woman: Woman)
    }

    abstract class Person {
        abstract fun accept(action: Action)
    }

    class Woman : Person() {
        override fun accept(action: Action) {
            action.getWomanResult(this)
        }
    }

    class Man : Person() {
        override fun accept(action: Action) {
            action.getManResult(this)
        }
    }

    class Success : Action() {
        override fun getManResult(man: Man) {
            println("男性评价该歌成功")
        }

        override fun getWomanResult(woman: Woman) {
            println("女性评价该歌成功")
        }
    }

    class Fail : Action() {
        override fun getManResult(man: Man) {
            println("男性评价该歌失败")
        }

        override fun getWomanResult(woman: Woman) {
            println("女性评价该歌失败")
        }
    }

    class ObjectStructure {
        private val persons = mutableListOf<Person>()

        fun attach(person: Person) {
            persons.add(person)
        }

        fun detach(person: Person) {
            persons.remove(person)
        }

        fun display(action: Action) {
            for (i in persons) {
                i.accept(action)
            }

        }
    }

}